/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * forEvaluation.java
 *
 * Created on Mar 2, 2011, 10:44:39 PM
 */

package crfsvm.crf.een_phuong;

import java.awt.Color;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;
import javax.swing.text.JTextComponent;
import java.io.*; //my addition
import javax.swing.UIManager;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;


/**
 *
 * @author Thien
 */
public class forEvaluation extends javax.swing.JFrame {

    /** Creates new form forEvaluation */
    public forEvaluation() {

        try
        {
            this.setTitle("Named Entity Recognition Application - Natural Language Processing Group - Hanoi University of Science and Technology");
            this.setIconImage(Toolkit.getDefaultToolkit().getImage("." + File.separator + "src" + File.separator + "een_phuong" + File.separator + "resources" + File.separator + "taggingService.JPG"));
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            initComponents();
        }
        catch (Exception e)
        {
            jLabel4.setText("Can not update Look and Feel: " + e);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea3 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea4 = new javax.swing.JTextArea();
        jButton4 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setWrapStyleWord(true);
        jTextArea1.setName("areaCRF"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea2.setColumns(20);
        jTextArea2.setEditable(false);
        jTextArea2.setLineWrap(true);
        jTextArea2.setRows(5);
        jTextArea2.setWrapStyleWord(true);
        jTextArea2.setName("areaLIAO"); // NOI18N
        jScrollPane2.setViewportView(jTextArea2);

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jTextArea3.setColumns(20);
        jTextArea3.setEditable(false);
        jTextArea3.setLineWrap(true);
        jTextArea3.setRows(5);
        jTextArea3.setWrapStyleWord(true);
        jTextArea3.setName("areaCoreferenceRule"); // NOI18N
        jScrollPane3.setViewportView(jTextArea3);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(forEvaluation.class);
        jButton1.setLabel(resourceMap.getString("buttonCRF.label")); // NOI18N
        jButton1.setName("buttonCRF"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crfButton_Click(evt);
            }
        });

        jButton2.setLabel(resourceMap.getString("buttonLIAO.label")); // NOI18N
        jButton2.setName("buttonLIAO"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crfLIAOButton_Click(evt);
            }
        });

        jButton3.setLabel(resourceMap.getString("buttonCoreferenceRule.label")); // NOI18N
        jButton3.setName("buttonCoreferenceRule"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crfCoreference_Click(evt);
            }
        });

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        jTextArea4.setColumns(20);
        jTextArea4.setLineWrap(true);
        jTextArea4.setRows(5);
        jTextArea4.setWrapStyleWord(true);
        jTextArea4.setName("areaInput"); // NOI18N
        jScrollPane4.setViewportView(jTextArea4);

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButton(evt);
            }
        });

        jTextField1.setText(resourceMap.getString("fieldPath.text")); // NOI18N
        jTextField1.setName("fieldPath"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setForeground(resourceMap.getColor("jLabel3.foreground")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton5.setText(resourceMap.getString("buttonSummit.text")); // NOI18N
        jButton5.setName("buttonSummit"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                summitButton_Click(evt);
            }
        });

        jLabel4.setFont(resourceMap.getFont("statusLabel.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("statusLabel.text")); // NOI18N
        jLabel4.setName("statusLabel"); // NOI18N

        jButton6.setText(resourceMap.getString("jButton6.text")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inspectAllButton_Click(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton5)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 707, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4)
                        .addGap(296, 296, 296))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 1100, Short.MAX_VALUE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 451, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(769, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton3)
                    .addComponent(jButton1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jButton6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 995, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 995, Short.MAX_VALUE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 995, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 362, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addGap(477, 477, 477))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addComponent(jButton5)))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(9, 9, 9)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addGap(130, 130, 130)
                        .addComponent(jButton3)
                        .addGap(68, 68, 68)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void browseButton(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButton
        // TODO add your handling code here:
        JFileChooser choose = new JFileChooser();
        ExampleFileFilter filter = new ExampleFileFilter("txt","txt File");
        choose.addChoosableFileFilter(filter);
        int f = choose.showOpenDialog(this);
        if (f == JFileChooser.APPROVE_OPTION) {
            File inFile = choose.getSelectedFile();
            jTextField1.setText(inFile.getPath());
        }


        BufferedReader br = null;
        try
        {
             String save = "", line = "";
             br = new BufferedReader(new InputStreamReader(new FileInputStream(jTextField1.getText()), "UTF-8"));
             while ((line = br.readLine()) != null)
             {
                 save += line + "\n";
             }

             br.close();
             jTextArea4.setText(save);
             
             CopyFile.copyfile(jTextField1.getText(), evaluatePath + File.separator + tempName);
             jLabel4.setText("Loaded input File!");
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
    }//GEN-LAST:event_browseButton

    private void crfButton_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crfButton_Click
        // TODO add your handling code here:
        try
        {
            File f = new File(modelFile);
            f.delete();
            CopyFile.copyfile(evaluatePath + File.separator + "model_crf.txt", modelFile);
            tagInput(jTextArea1);
            jLabel4.setText("Done CRF!");
        }
        catch (Exception ex)
        {
            System.out.println(ex.toString());
        }
    }//GEN-LAST:event_crfButton_Click

    private void crfLIAOButton_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crfLIAOButton_Click
        // TODO add your handling code here:
        try
        {
            File f = new File(modelFile);
            f.delete();
            CopyFile.copyfile(evaluatePath + File.separator + "model_liao.txt", modelFile);
            tagInput(jTextArea2);
            jLabel4.setText("Done CRF-LIAO!");
        }
        catch (Exception ex)
        {
            jLabel4.setText("Error: " + ex.toString());
        }
    }//GEN-LAST:event_crfLIAOButton_Click

    private void crfCoreference_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crfCoreference_Click
        // TODO add your handling code here:
        try
        {
            File f = new File(modelFile);
            f.delete();
            CopyFile.copyfile(evaluatePath + File.separator + "model_coreference.txt", modelFile);
            tagInput(jTextArea3);
            jLabel4.setText("Done CRF-Coreference!");
        }
        catch (Exception ex)
        {
            jLabel4.setText("Error: " + ex.toString());
        }
    }//GEN-LAST:event_crfCoreference_Click

    private void summitButton_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_summitButton_Click
        // TODO add your handling code here:
        try
        {
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(evaluatePath + File.separator + tempName), "UTF-8"));
            bw.write(jTextArea4.getText());
            bw.close();
            jLabel4.setText("Status: Summitted Successfully!");
        }
        catch (Exception ex)
        {
            jLabel4.setText("Error: " + ex.toString());
        }
    }//GEN-LAST:event_summitButton_Click

    private void inspectAllButton_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inspectAllButton_Click
        // TODO add your handling code here:
        try
        {
            File f = new File(modelFile);
            f.delete();
            CopyFile.copyfile(evaluatePath + File.separator + "model_coreference.txt", modelFile);
            tagInput(jTextArea1);
            tagInput(jTextArea2);
            tagInput(jTextArea3);
            jLabel4.setText("Done Inspect All!");
        }
        catch (Exception ex)
        {
            jLabel4.setText("Error: " + ex.toString());
        }
    }//GEN-LAST:event_inspectAllButton_Click

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new forEvaluation().setVisible(true);
            }
        });
    }

    class nameEntityForConfidence //copied from EEN_PhuongView.java
    {
        public String type; //time, per,org, loc
        public String instance;
        public int beginIdx;
        public int endIdx;
        public String confidence;
    }

    //copied from EEN_PhuongView.java
    private nameEntityForConfidence getNextEntityConfidence(String data, int curpos) {
        nameEntityForConfidence nent = null;

        int beginOpenTag = data.indexOf("<", curpos);
        int endOpenTag = data.indexOf(">", beginOpenTag);

        if (beginOpenTag == -1 || endOpenTag == -1) {
            return null;
        }

        String temp = data.substring(beginOpenTag + 1, endOpenTag).toLowerCase();
        int index = temp.indexOf(" ");
        String openTag = temp.substring(0, index);
        String confi = temp.substring(index + 1);
        String closeTag = "</" + openTag + ">";

        int closeTagIdx = data.indexOf(closeTag, endOpenTag + 1);

        if (closeTagIdx == -1) {
            return null;
        }

        nent = new nameEntityForConfidence();

        nent.beginIdx = beginOpenTag;
        nent.endIdx = closeTagIdx + closeTag.length();
        nent.instance = data.substring(endOpenTag + 1, closeTagIdx).trim();
        nent.type = openTag;
        nent.confidence = confi;

        return nent;
    }

    private  boolean checkPunctuation(String line)
    {
        boolean result = false;
         String[] punctuations = {".", "," , "!", "(", ")", "[", "]", "{", "}", "$", "?", "@", "\"", "-", "/", "...", ":", "'", ";", "*", "+" , "#",
        "%", "^", "&", "=", "|", "~", "`"};

         for (int i = 0; i < punctuations.length; i++)
             if (punctuations[i].equals(line))
             {
                 result = true;
                 break;
             }

         return result;
    }

    public void highlight(JTextComponent textComp, String pattern, int i, Highlighter hilite, String color) throws BadLocationException {
//        removeHighlights(textComp);
        try {
            Document doc = textComp.getDocument();
            String text = doc.getText(0, doc.getLength());
//            text = text.substring(i, text.length());
            int pos = i;
            while ((pos = text.indexOf(pattern, pos)) >= 0) {
                if (color.equalsIgnoreCase("num")) {
                    hilite.addHighlight(pos, pos + pattern.length(), numHighlightPainter);
                    pos += pattern.length();
                } else if (color.equalsIgnoreCase("time")) {
                    hilite.addHighlight(pos, pos + pattern.length(), timeHighlightPainter);
                    pos += pattern.length();
                } else if (color.equalsIgnoreCase("per")) {
                    hilite.addHighlight(pos, pos + pattern.length(), perHighlightPainter);
                    pos += pattern.length();
                } else if (color.equalsIgnoreCase("misc")) {
                    hilite.addHighlight(pos, pos + pattern.length(), miscHighlightPainter);
                    pos += pattern.length();
                } else if (color.equalsIgnoreCase("pct")) {
                    hilite.addHighlight(pos, pos + pattern.length(), pctHighlightPainter);
                    pos += pattern.length();
                } else if (color.equalsIgnoreCase("cur")) {
                    hilite.addHighlight(pos, pos + pattern.length(), curHighlightPainter);
                    pos += pattern.length();
                } else if (color.equalsIgnoreCase("loc")) {
                    hilite.addHighlight(pos, pos + pattern.length(), locHighlightPainter);
                    pos += pattern.length();
                } else if (color.equalsIgnoreCase("org")) {
                    hilite.addHighlight(pos, pos + pattern.length(), orgHighlightPainter);
                    pos += pattern.length();
                }
            }
        } catch (BadLocationException e) {
        }
    }
    Highlighter.HighlightPainter numHighlightPainter = (HighlightPainter) new MyHighlightPainter(Color.GREEN);
    Highlighter.HighlightPainter timeHighlightPainter = (HighlightPainter) new MyHighlightPainter(Color.RED);
    Highlighter.HighlightPainter perHighlightPainter = (HighlightPainter) new MyHighlightPainter(Color.CYAN);
    Highlighter.HighlightPainter miscHighlightPainter = (HighlightPainter) new MyHighlightPainter(Color.YELLOW);
    Highlighter.HighlightPainter pctHighlightPainter = (HighlightPainter) new MyHighlightPainter(Color.GRAY);
    Highlighter.HighlightPainter curHighlightPainter = (HighlightPainter) new MyHighlightPainter(Color.blue);
    Highlighter.HighlightPainter locHighlightPainter = (HighlightPainter) new MyHighlightPainter(Color.LIGHT_GRAY);
    Highlighter.HighlightPainter orgHighlightPainter = (HighlightPainter) new MyHighlightPainter(Color.MAGENTA);

    class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {

        public MyHighlightPainter(Color color) {
            super(color);
        }
    }

    private void tagInput(javax.swing.JTextArea textArea) {
        // TODO add your handling code here:
         try {

            textArea.setText("");
            String tagg = "." + File.separator + "vnTagger" + File.separator + "input.txt";
            CopyFile.copyfile(evaluatePath + File.separator + tempName, tagg);
            vnTokenizer vntokenizer = new vnTokenizer();
            try {
                //vntokenizer.token();
                tokenizeVietnamese.token();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(EEN_PhuongView.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(EEN_PhuongView.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(EEN_PhuongView.class.getName()).log(Level.SEVERE, null, ex);
            }

            JVnRecognizer jVnRecognizer = new JVnRecognizer();
            String[] args = new String[4];
            args[0] = "-modeldir";
            args[1] = "./model";
            args[2] = "-inputfile";
            args[3] = "input.txt";
            jVnRecognizer.main(args);
            BufferedReader in = null;
            String line = null;
            try {
                try {
                    in = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt.wseg"), "UTF-8"));
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(EEN_PhuongView.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(EEN_PhuongView.class.getName()).log(Level.SEVERE, null, ex);
            }
            String ret = "", myret = "";
            Highlighter hilite = textArea.getHighlighter();

            //My code
            File f = new File(jTextField1.getText());
            String myPath = "." + File.separator + "data" + File.separator + "mypredicted" + File.separator + f.getName() + ".predicted";
            BufferedWriter predict = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(myPath), "UTF-8"));
            //End my code

            try {
                while ((line = in.readLine()) != null) {
                    if (line.trim().length() == 0) {
                        ret += "\n\n";
                        myret += "\n\n";
                        continue;
                    }
                    //NamedEntity nent;
                    nameEntityForConfidence nent;
                    int curpos = 0;
                    while ((nent = getNextEntityConfidence(line, curpos)) != null) { //nent = getNextEntity(line, curpos)
                        String aheadStr = line.substring(curpos, nent.beginIdx);
                        VnStringTokenizer tk = new VnStringTokenizer(aheadStr, " {}");
                        while (tk.hasMoreTokens()) {
                            String token = tk.nextToken();
                            // my code
                            if (checkPunctuation(token))
                                myret += token + " ";
                            else
                                myret += "[" + token + "] ";
                            //end my code
                            token += " ";
                            ret += token;
                            //myret += token;
                            textArea.append(token);
                        }
                        // myret += "<" + nent.type + ">" + " ";
                        //my code
                        myret += "<" + nent.type + ">" + " ";
                        //ret += "<" + nent.type + " " + nent.confidence + ">" + " ";
                        //jTextArea1.append("<" + nent.type + " " + nent.confidence + ">" + " ");
                        //end my code
                        VnStringTokenizer entTk = new VnStringTokenizer(nent.instance, " {}");
                        while (entTk.hasMoreTokens()) {
                            String token = entTk.nextToken();
                            // my code
                            if (checkPunctuation(token))
                                myret += token + " ";
                            else
                                myret += "[" + token + "] ";
                            //end my code
                            String retOld = ret;
                            token += " ";
                            ret += token;
                            //myret += token;
                            textArea.append(token);
                            try {
                                highlight(textArea, token.trim(), retOld.length(), hilite, nent.type);
                            } catch (BadLocationException ex) {
                                Logger.getLogger(EEN_PhuongView.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        //my code
                        myret += "</" + nent.type + ">" + " ";
                        //ret += "</" + nent.type + ">" + " ";
                        //jTextArea1.append("</" + nent.type + ">" + " ");
                        //end my code

                        curpos = nent.endIdx + 1;
                    }
                    if (curpos < line.length()) {
                        String remain = line.substring(curpos, line.length());
                        VnStringTokenizer tk = new VnStringTokenizer(remain, " ");
                        while (tk.hasMoreTokens()) {
                            String token = tk.nextToken();
                            //System.out.println("token = " + token);
                            // my code
                            if (checkPunctuation(token))
                                myret += token + " ";
                            else
                                myret += "[" + token + "] ";
                            //end my code
                            token += " ";
                            ret += token;
                            //myret += token;
                            textArea.append(token);
                        }
                    }
                    textArea.append("\n\n");
                }
                predict.write(myret);
                predict.close();
            } catch (IOException ex) {
                Logger.getLogger(EEN_PhuongView.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(EEN_PhuongView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(EEN_PhuongView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextArea jTextArea3;
    private javax.swing.JTextArea jTextArea4;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    private String evaluatePath = "." + File.separator + "model" + File.separator + "forEvaluation";
    private String modelFile = "." + File.separator + "model" + File.separator + "model.txt";
    private String tempName = "inputForEvaluation.txt";
}
